package com.bulletelc.apollopre.module.main.classify;

import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.Classify;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/13
 * desc  : TODO
 */

public class ClassifyPresenter extends BasePresenter<ClassifyContract.View> implements ClassifyContract.Presenter {

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getClassifyFirst() {
        OkGo.<String>get(NetWorkPath.GET_PRODUCT_CATEGORY)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //LogUtil.i(response.body().toString());
                        parseFirst(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorFirst(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 获取二级分类
     * @param cid
     */
    @Override
    public void getClassifySecond(String cid) {
        OkGo.<String>get(NetWorkPath.GET_PRODUCT_CATEGORY)
                .params("cid",cid)
                .params("depth",1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //LogUtil.i(response.body().toString());
                        parseSecond(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });
    }

    /**
     * 解析一级分类数据
     */
    private void parseFirst(Response<String> response) {
        List<Classify> classifyList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response.body().toString());
            JSONArray dataArray = object.getJSONArray("data");
            for (int i = 0, size = dataArray.length(); i < size; i++) {
                Classify classify = new Classify();
                classify.setId(dataArray.getJSONObject(i).getString("id"));
                classify.setName(dataArray.getJSONObject(i).getString("name"));
                classify.setImageUrl(dataArray.getJSONObject(i).getString("mediumUrl"));
                classifyList.add(classify);
            }
            if (isViewAttached()) {
                getView().onSuccessFirst(classifyList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析二级分类
     * @param response
     */
    private void parseSecond(Response<String> response) {
        List<Classify> classifyList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response.body().toString());
            JSONArray dataArray = object.getJSONArray("data");
            for (int i = 0, size = dataArray.length(); i < size; i++) {
                Classify classify = new Classify();
                classify.setId(dataArray.getJSONObject(i).getString("id"));
                classify.setName(dataArray.getJSONObject(i).getString("name"));
                classify.setImageUrl(dataArray.getJSONObject(i).getString("originalUrl"));
                JSONArray categories = dataArray.getJSONObject(i).getJSONArray("childrenCategories");
                List<Classify> classifyList1 = new ArrayList<>(); // 三级分类
                if (categories.length() > 0) {
                    for (int j = 0; j < categories.length(); j++) {
                        Classify classify1 = new Classify();
                        classify1.setId(categories.getJSONObject(j).getString("id"));
                        classify1.setName(categories.getJSONObject(j).getString("name"));
                        classify1.setImageUrl(categories.getJSONObject(j).getString("mediumUrl"));
                        classifyList1.add(classify1);
                    }
                }
                classify.setClassifyList(classifyList1);
                classifyList.add(classify);
            }
            if (isViewAttached()) {
                getView().successSecond(classifyList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
